package com.bjpowernode.seckillservice.config;

import com.bjpowernode.constant.Constants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟设置  超时未支付
 */
@Configuration
public class DelayConfig {

    /**
     * 自定义的交换机
     * 要使用延迟插件
     */
    @Bean
    public CustomExchange customExchange() {
        //交换机的配置
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");

        return new CustomExchange(Constants.EXCHANGE_DELAY_NAME, "x-delayed-message", true, false, args);
    }

    /**
     * 配置队列
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(Constants.QUEUE_DELAY_NAME);
    }

    /**
     * 绑定队列和交换机
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(delayQueue()).to(customExchange()).with(Constants.QUEUE_DELAY_NAME).noargs();
    }
}
